import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import StorageUtils from './../../../app/storage';
import update from 'react-addons-update';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    //是否锁定
    readOnly: false,
    //保存成功弹出提示
    showToptips: false,
    showWarn: false,
    warnTimer: null,
    codeTimer: null,
    warnMessage: '',
    qrInfo:{},
    userInfo: {
      certNum: '',
      telNum: '',
      vcode: '',
      name:'',
      organName:''
    }

  },

  initMode: function () {
    let t = this;
    this.data.isUpload = update(this.data.isUpload,  {$set: false});
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    t.updateComponent();
  },

  loadMode: function (params) {
    let t = this;
    this.data.qrInfo = update(this.data.qrInfo,  {$set: params});
    t.updateComponent();
  },

  uplogin:function () {
    console.info("提交");
    let t = this;
    this.data.isUpload = update(this.data.isUpload,  {$set: true});
      DB.WechatAPI.uplogin({
        qrcode: this.data.qrInfo.resultStr,
        openId: this.data.userInfo.openId,
        actorId: this.data.userInfo.telNum,
      }).then(
        function (content) {
          console.log("1");
          t.data.showCertOK = update(t.data.showCertOK, {$set: true});
          console.log("3");
          t.updateComponent();
        },
        function (error) {
          console.log(error);
        });

  },


  showWarnMessage: function (message) {

    this.data.warnMessage = update(this.data.warnMessage, {$set: message});
    this.data.showWarn = update(this.data.showWarn, {$set: true});
    this.data.warnTimer = setTimeout(()=> {
      this.data.showWarn = update(this.data.showWarn, {$set: false});
      this.updateComponent();
    }, 3000);

    this.updateComponent();
  },

  onUpdateStore: function () {
    let t = this;
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
